<?php
/**
* Process the user login by checking username/password against the DB
*
* @author       Fabrice Douteaud <clearbudget@douteaud.com>
* @package      actions
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice douteaud (clearbudget@douteaud.com)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/
$badUsername = false;
$badPassword = false;
$logoutSuccess = false;

// get the input values
isset($_REQUEST['username'])?$username = $_REQUEST['username']:$username = null;
isset($_REQUEST['password'])?$password = $_REQUEST['password']:$password = null;
isset($_REQUEST['backAction'])?$backAction = $_REQUEST['backAction']:$backAction = null;
class_debug::addMsg(__FILE__, 'backAction is "'.$backAction.'"', DEBUGDEBUG);

// if the user is logged in, we log him out
if (class_context::isUserLoggedIn()) {
  $logoutSuccess = true;
  setcookie('user', '');
  }

// if the user did not submit, we simply go to the view
if(!isset($_REQUEST['submit'])) {
  return;
  }

if($username != class_settings::$username || $password != class_settings::$password) {
   $badUsername = true;
   $badPassword = true;
   }
 else {
   setcookie('user', 'true');
   // next is to go to the previous action
   if($backAction == '' || $backAction == 'login') $backAction = 'tableOverview';
   class_context::$nextAction = $backAction;
   class_context::$snippet = $backAction;
   // unset the submit flag
   $_REQUEST['submit'] = null;
   }
?>